package com.atguigu.zy.interview.juc.queue;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 资源类
 */
class ShareData {
    private Integer number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    /**
     * 1、condition需要写在lock块中
     */
    public void increment() throws InterruptedException {
        lock.lock();
        try {
            //1、判断
            //等于0件开始生产，取反，为了保证循环代码最少
            while (number != 0) {
                //等待，不能生产
                condition.await();
            }
            number++;
            //2、干活
            System.out.println(Thread.currentThread().getName() + "\t " + number);
            //3、通知唤醒
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            //1、判断
            //不等于0开始消费
            while (number == 0) {
                //等待，不能消费
                condition.await();
            }
            number--;
            //2、干活
            System.out.println(Thread.currentThread().getName() + "\t " + number);
            //3、通知唤醒
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

}

/**
 * 深透明细
 * 传统的生产者消费者模式
 * @author Jungle
 * @formatter:off
 * 题目:一个初始值为零的变量(对象中 )，两个线程对其交替操作，一个加1一个减1，来5轮
     * 1、线程     燥作(方法)     资源类
     * 2、判晰     于活    通知
     * 3、防止虚假唤醒机制 （用while）  用if在两个线程没有问题，但是4个线程就会出问题
 * @formatter:on
 */
public class ProdConsumerTraditionDemo {
    public static void main(String[] args) {
        ShareData shareData = new ShareData();
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    shareData.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "AA").start();


        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    shareData.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "BB").start();


        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    shareData.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "CC").start();


        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    shareData.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "DD").start();

    }
}
